//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 4
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace UpdateFolderSetProperty
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            FolderIdType folderId = EWSHelpers.CreateFolder(
                            binding,
                            "FolderToBeUpdated",
                            new DistinguishedFolderIdType(
                                DistinguishedFolderIdNameType.inbox));

            UpdateFolderSet(
                        binding,
                        folderId);
        }

        /// <summary>
        /// Update the passed folder's display name and folder class (Listing 4-17)
        /// </summary>
        /// <param name="binding">Binding to use</param>
        /// <param name="folderIdToChange">Id of folder to change</param>
        /// 
        public static void UpdateFolderSet(
                   ExchangeServiceBinding binding, 
                   FolderIdType folderIdToChange)
        {
            // Create the request
            //
            UpdateFolderType request = new UpdateFolderType();

            // Create our single folder change element (we are only updating a 
            // single folder)
            //
            FolderChangeType folderChange = new FolderChangeType();

            // Here is another "Item" property.  This one is for the folder id to 
            // update
            //
            folderChange.Item = folderIdToChange;

            // We are updating two properties
            //
            folderChange.Updates = new FolderChangeDescriptionType[2];

            // Now let's fill in the info for the display name update
            //
            SetFolderFieldType displayNameUpdate = new SetFolderFieldType();

            // Set the path first
            //
            PathToUnindexedFieldType displayNamePath = 
                new PathToUnindexedFieldType(
                    UnindexedFieldURIType.folderDisplayName);
            
            displayNameUpdate.Item = displayNamePath;

            // Now set the state
            //
            FolderType folderDisplayNameState = new FolderType();
           
            // Remember that we can only set a single property and it must match our 
            // path from above
            //

            folderDisplayNameState.DisplayName = "New Folder Name";

            // That's exciting, now we have an Item1 too!  This one refers to the 
            // Folder state to update
            //
            displayNameUpdate.Item1 = folderDisplayNameState;

            // And again for the FolderClass (less comments this time)
            //
            SetFolderFieldType folderClassUpdate = new SetFolderFieldType();
            PathToUnindexedFieldType folderClassPath =
                new PathToUnindexedFieldType(
                    UnindexedFieldURIType.folderFolderClass);
            folderClassUpdate.Item = folderClassPath;

            FolderType folderClassState = new FolderType();
            folderClassState.FolderClass = "IPF.Note.Contoso";
            folderClassUpdate.Item1 = folderClassState;

            // Now add the two field changes that we just created to the updates 
            // array
            //
            folderChange.Updates[0] = displayNameUpdate;
            folderChange.Updates[1] = folderClassUpdate;

            request.FolderChanges = new FolderChangeType[] { folderChange };

            // Make the call and extract the response
            //
            UpdateFolderResponseType response = binding.UpdateFolder(request);
            FolderInfoResponseMessageType responseMessage = 
               response.ResponseMessages.Items[0] as FolderInfoResponseMessageType;
           
            Console.WriteLine(responseMessage.Folders[0].FolderId.Id.ToString());
        }
    }
}
